package com.example.demo.dao.business;
import com.example.demo.model.business.Order;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface OrderDAO {




    //    一对一
    @Select("select * from order where id=#{order_id}")
    List<Order> getByOrderId(Integer order_id);


    //查询全部
    @Select("select * from `order`")
    @Result(column = "employee_name",property = "employee_name")
    @Result(column = "employee_id", property = "employee",
            one = @One(select = "com.example.demo.dao.people.EmployeeDAO.getByEmployeeId",fetchType = FetchType.LAZY))
    @Result(column = "customer_id", property = "customer",
            one = @One(select = "com.example.demo.dao.people.CustomerDAO.selectByCustomerId",fetchType = FetchType.LAZY))
    @Result(column = "product_id", property = "product",
            one = @One(select = "com.example.demo.dao.warehouse.ProductDAO.getByProductId",fetchType = FetchType.LAZY))
    List<Order> selectAll();

    //    添加数据
    @Insert("insert into `order`(customer_id,order_number,order_sum,employee_id,order_status,product_id)values(#{customer_id},#{order_number},#{order_sum},#{employee_id},'0',#{product_id})")
    Integer addOrder(Order order);

    //删除数据
    @Delete("delete from `order` where order_id=#{order_id}")
    Integer deleteById(Integer order_id);

    //    修改数据
    @Update("update order set customer_id=#{customer_id},order_time=#{order_time},order_number=#{order_number},order_sum=#{order_sum},employee_id=#{employee_id} where order_id=#{order_id}")
    Integer updateById(Order order);

    @Update("update `order` set order_status=1 where order_id=#{order_id}")
    Integer updateStatusById(Integer order_id);

    //  模糊查询
    @Select("<script>"+
            "select * from `order`" +
            "<where>"+
            "<if test=\"order_number !=null and order_number != ''\">"+
            "and order_number like CONCAT('%',#{order_number},'%')"+
            "</if>"+
            "<if test=\"customer_name !=null and customer_name != ''\">"+
            "and customer_name like CONCAT('%',#{customer_name},'%')"+
            "</if>"+
            "</where>"+
            "</script>")
    @Result(column = "employee_id", property = "employee",
            one = @One(select = "com.example.demo.dao.people.EmployeeDAO.getByEmployeeId",fetchType = FetchType.LAZY))
    @Result(column = "customer_id", property = "customer",
            one = @One(select = "com.example.demo.dao.people.CustomerDAO.selectByCustomerId",fetchType = FetchType.LAZY))
    @Result(column = "product_id", property = "product",
            one = @One(select = "com.example.demo.dao.warehouse.ProductDAO.getByProductId",fetchType = FetchType.LAZY))
    public List<Order> findOrder(Integer order_number,String  customer_name) ;
}
